package com.xinzhi.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.xinzhi.mapper.AppraisalMapper;
import com.xinzhi.mapper.PointAppraisalMapper;
import com.xinzhi.pojo.dto.Dtokoufen;
import com.xinzhi.pojo.dto.Dtoshensukou;
import com.xinzhi.pojo.dto.Teshujiafens;
import com.xinzhi.service.AppraisalService;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppraisalServiceImpl implements AppraisalService {
    @Autowired
    private AppraisalMapper appraisalMapper;
    @Autowired
    private PointAppraisalMapper pointAppraisalMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 特殊加分申请查询接口
     * @param object
     * @return
     */
    @Override
    public Result getAppraisalByObject(String object) {
        String rediskay = "userlist"+object;
        //先去Redis中查询数据
        String userlist = (String) redisUtils.get(rediskay);
        //进行Redis判断
        if (StrUtil.isNotEmpty(userlist)){
            return Result.ok(userlist);
        }

        //对参数进行判断
        if (object ==null){
            return Result.error("查询标题不能为空");
        }
        //进行数据库查询
        List<Teshujiafens> appraisalByObject = appraisalMapper.getAppraisalByObject(object);
        if (appraisalByObject !=null){
            redisUtils.set(rediskay, JSONUtil.toJsonStr(appraisalByObject));
            return Result.ok(appraisalByObject);
        }else {
            return Result.error("数据库中没有这条数据");
        }
    }

    @Override
    public Result AppraisalService(String object) {
//先去redis里查询这条数据存不存在
        String userKey = "user:" +object;
        String existingUserJson = (String) redisUtils.get(userKey);
        if (StrUtil.isNotBlank(existingUserJson)) {
            return Result.ok(existingUserJson);
        }
        if (object==null){
            return Result.error("没有数据");
        }
// //如果redis里没有，则从数据库中查询
// List byAccount1 = appraisalMapper.findByAccount1(object);
        List<Dtokoufen> querystudent = appraisalMapper.querystuden(object);

        if (querystudent != null){
//将查询到的数据存入redis中
            redisUtils.set(userKey, JSONUtil.toJsonStr(querystudent));
            return Result.ok(querystudent); }
        return Result.error("没有数据");
    }

    @Override
    public Result updateAppraisal(String demerit ,Integer deleted) {
        if (deleted != null && deleted == 1) {
            int updateappraisal = appraisalMapper.updateappraisal(demerit, deleted);
            return updateappraisal > 0 ? Result.ok("删除成功") : Result.error("删除失败");
        } else {
            return Result.error("删除失败");
        }
    }

    //特殊扣分申请查询全部
    @Override
    public Result selectAlls( ) {
        List<Dtoshensukou> appraisals = pointAppraisalMapper.querystuden2();
        return Result.ok(appraisals);
    }


    /**
     * 特殊扣分申请根据年份查询
     * @param object
     * @param year
     * @return
     */

    @Override
    public Result selectAllsByYear(String object, String year) {
        System.out.println("我是沙尔比");
        if (object == null || year == null){
            return Result.error("查询条件不能为空");
        }
        List<Dtokoufen> dtokoufens = appraisalMapper.querystuden2(object, year);
        if (dtokoufens == null){
            return Result.error("没有相关数据");
        }
        return dtokoufens != null ? Result.ok(dtokoufens) : Result.error("没有相关数据");
    }
}
